<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <!--      <el-form-item label="属性名称" prop="attributeName">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.attributeName"-->
      <!--          placeholder="请输入属性名称"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="产品名" prop="productName">
        <el-input
          v-model="queryParams.productName"
          clearable
          placeholder="请输入产品名"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">{{$t("search")}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t("reset")}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="attributeList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="产品名" prop="productName"/>

      <el-table-column align="center" label="公有属性" prop="productCode"/>

      <el-table-column align="center" class-name="small-padding" :label="$t('createTime')" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" :label="$t('controls')">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:attribute:edit']"
            icon="el-icon-view"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >查看
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['system:attribute:remove']"-->
          <!--          >{{$t("delete")}}</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改产品属性对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body center width="500px">
      <el-form ref="form" :disabled=open :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品名称" prop="attributeId">
          <el-input v-model="form.attributeId" placeholder="请输入属性名称"/>
        </el-form-item>
        <el-form-item label="公有属性" prop="attributeNamee">
          <el-input v-model="form.attributeNamee" placeholder="请输入属性英文"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">

        <el-button @click="cancel">{{$t("cancel")}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addAttribute, delAttribute, listAttributes, updateAttribute} from "@/api/system/attribute";

export default {
  name: "Attribute",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品属性表格数据
      attributeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        attributeName: null,
        attributeNamee: null,
      },
      // 表单参数
      form: {},
      // 表单校验

    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime(time, pattern) {
      if (arguments.length === 0 || !time) {
        return null
      }
      const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
      let date
      if (typeof time === 'object') {
        date = time
      } else {
        if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
          time = parseInt(time)
        } else if (typeof time === 'string') {
          time = time.replace(new RegExp(/-/gm), '/');
        }
        if ((typeof time === 'number') && (time.toString().length === 10)) {
          time = time * 1000
        }
        date = new Date(time)
      }
      const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay(),
      }
      // 分隔符是竖杠|不是斜杠/,注意此正则表达式 /{(y|m|d|h|i|s|a)+}/g
      const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        let value = formatObj[key]
        // Note: getDay() returns 0 on Sunday
        if (key === 'a') {
          return ['日', '一', '二', '三', '四', '五', '六'][value]
        }
        if (result.length > 0 && value < 10) {
          value = '0' + value
        }
        return value || 0
      })
      return time_str;
    },
    /** 查询产品属性列表 */
    getList() {
      this.loading = true;
      listAttributes(this.queryParams).then(response => {
        this.attributeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        attributeId: null,
        attributeName: null,
        attributeNamee: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.attributeId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品属性";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const attributeId = row.attributeId || this.ids

      this.form.attributeId = row.productName
      this.form.attributeNamee = row.productCode

      this.open = true;
      this.title = "查看属性";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.attributeId != null) {
            updateAttribute(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('modifiedSuccessfully'));
              this.open = false;
              this.getList();
            });
          } else {
            addAttribute(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('newSuccess'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const attributeIds = row.attributeId || this.ids;
      this.$modal.confirm('是否确认删除产品属性编号为"' + attributeIds + this.$t('dataItem')).then(function () {
        return delAttribute(attributeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('successfullyDeleted'));
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/attribute/export', {
        ...this.queryParams
      }, `attribute_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
